package es.uji.viselab.image.source.kinect;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JPanel;

import processing.core.PApplet;
import es.uji.viselab.ViSeLabException;
import es.uji.viselab.experiment.ExperimentParameters;
import es.uji.viselab.image.source.ImageSource;

public class Kinect extends ImageSource implements ItemListener  {

	public Kinect(ExperimentParameters ep) {
		super(ep);
		// TODO Auto-generated constructor stub
	}


	protected JCheckBox checkBoxKinectVisualServoing;
	protected JCheckBox checkBoxKinectDepth;
	protected JCheckBox checkBoxKinectSkeleton;
	protected JCheckBox checkBoxKinectOpenGL;

	PApplet kinectProcessing;

	public JPanel getImagePanel() throws ViSeLabException {
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());

		JPanel menuPanel = getMenuPanel();
		JPanel kinectProcessingPanel = getKinectProcessingPanel();

		panel.add(menuPanel, BorderLayout.NORTH);
		panel.add(kinectProcessingPanel, BorderLayout.CENTER);

		return panel;
	}

	
	protected JPanel getKinectProcessingPanel() throws ViSeLabException {
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		kinectProcessing = new KinectProcessing();
		panel.add(kinectProcessing);
		if (((KinectProcessing) kinectProcessing).isConnected()){
		   kinectProcessing.init();
		} else {
		   throw new ViSeLabException("Kinect is not connected");	
		}
		
		return panel;
	}

	protected JPanel getMenuPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());

		checkBoxKinectVisualServoing = new JCheckBox("Gather data");
		checkBoxKinectDepth = new JCheckBox("3D Depth");
		checkBoxKinectOpenGL = new JCheckBox("OpenGL");
		checkBoxKinectSkeleton = new JCheckBox("Skeleton");

		panel.add(checkBoxKinectVisualServoing);
		panel.add(checkBoxKinectDepth);
		panel.add(checkBoxKinectOpenGL);
		panel.add(checkBoxKinectSkeleton);

		checkBoxKinectVisualServoing.addItemListener(this);
		checkBoxKinectDepth.addItemListener(this);
		checkBoxKinectOpenGL.addItemListener(this);
		checkBoxKinectSkeleton.addItemListener(this);

		return panel;
	}

	@Override
	public void itemStateChanged(ItemEvent ie) {
		if (ie.getSource().equals(checkBoxKinectDepth)) {
			if (checkBoxKinectDepth.isSelected()) {
				((KinectProcessing) kinectProcessing)
						.activate(KinectFunctions.DEPTH);
				checkBoxKinectSkeleton.setEnabled(false);
				checkBoxKinectOpenGL.setEnabled(false);
			} else {
				((KinectProcessing) kinectProcessing)
				.deActivate(KinectFunctions.DEPTH);
				checkBoxKinectSkeleton.setEnabled(true);
				checkBoxKinectOpenGL.setEnabled(true);
			}
		}
		if (ie.getSource().equals(checkBoxKinectVisualServoing)) {
			if (checkBoxKinectVisualServoing.isSelected()) {
				((KinectProcessing) kinectProcessing)
						.activate(KinectFunctions.VISUALSERVOING);
				checkBoxKinectOpenGL.setEnabled(false);
			}else {
				((KinectProcessing) kinectProcessing)
				.deActivate(KinectFunctions.VISUALSERVOING);
				//checkBoxKinectOpenGL.setEnabled(true);
			}
		}
		if (ie.getSource().equals(checkBoxKinectOpenGL)) {
			if (checkBoxKinectOpenGL.isSelected()) {
				((KinectProcessing) kinectProcessing)
						.activate(KinectFunctions.OPENGL);
				checkBoxKinectSkeleton.setEnabled(false);
				checkBoxKinectVisualServoing.setEnabled(false);
				checkBoxKinectDepth.setEnabled(false);
			}else {
				((KinectProcessing) kinectProcessing)
				.deActivate(KinectFunctions.OPENGL);
				checkBoxKinectSkeleton.setEnabled(true);
				checkBoxKinectVisualServoing.setEnabled(true);
				checkBoxKinectDepth.setEnabled(true);
			}
		}
		if (ie.getSource().equals(checkBoxKinectSkeleton)) {
			if (checkBoxKinectSkeleton.isSelected()) {
				((KinectProcessing) kinectProcessing)
						.activate(KinectFunctions.SKELETON);
				checkBoxKinectOpenGL.setEnabled(false);
				checkBoxKinectDepth.setEnabled(false);
			}else {
				((KinectProcessing) kinectProcessing)
				.deActivate(KinectFunctions.SKELETON);
				checkBoxKinectOpenGL.setEnabled(true);
				checkBoxKinectDepth.setEnabled(true);
			}
		}
	}


	@Override
	public JPanel getPanel() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
	
}
